<?php
namespace Mobile\Model;
use Think\Model;
//订单
class OrderModel extends Model {
	/**
	* 获取用户所有订单
	* @param int $user_id 用户ID
	* @param string $type 订单 状态，1为待支付，2为待发货，3为待收货,4为已完成，5为已取消
	*/
	public function getList($user_id){
        $left = C('DB_PREFIX');
        $field = 'a.time,a.order_status,b.id,b.price,b.cart,c.store_name,c.s_id';
        $sql = 
            'SELECT '.$field .' FROM '.$left.'order as a 
            LEFT JOIN '.$left.'order_detail as b on a.order_number=b.order_number 
            LEFT JOIN '.$left.'store as c ON b.shop_id=c.s_id 
            WHERE a.user_id="'.$user_id.'" 
            order by a.time desc';
        $data = $this->query($sql);

        $status = array(1=>'待支付',2=>'已支付',3=>'待收货',4=>'已完成',5=>'已取消');
        foreach ($data as $key => &$value) {
        	$value['date'] = date('Y-m-d H:i',$value['time']);
            $value['status'] = $status[$value['order_status']];
            if($value['cart']){
                $cart = array();
            	$cart = json_decode($value['cart'],true);
                $productID = array();

            	foreach ($cart as $id => $num) {
                    $productID[] = $id;
            	}

                $productID = implode(',',$productID);
                $product = array();

                $sql = "
                select * from sp_goods_spec_option  as a
                LEFT JOIN sp_goods as b ON a.goods_id=b.goods_id 
                where a.goods_option_id in($productID)";
            	$product = $this->query($sql);
       
                foreach ($product as $k => &$v) {
                    $v['price'] = $cart[$v['goods_option_id']] * $v['price'];
                    $v['product_num'] = $cart[$v['goods_option_id']];
                    $v['goods_img']  = str_replace("./Uploads",'http://commission.weiyuekj.com/Uploads',$v['default_img']);
                }
                $value['product'] = $product;
            }
        }


		return $data;

	}

        /**
        * 获取用户所有订单
        * @param int $user_id 用户ID
        * @param string $type 订单 状态，1为待支付，2为待发货，3为待收货,4为已完成，5为已取消
        */
        public function getPayOrder(){
        $left = C('DB_PREFIX');
        $field = 'a.time,a.order_status,b.id,b.price,b.cart,c.shop_name,c.logo';
        $sql = 'SELECT '.$field .' FROM '.$left.'order as a LEFT JOIN '.$left.'order_detail as b on a.order_number=b.order_number LEFT JOIN '.$left.'shop as c ON b.shop_id=c.id where a.pay_status=2  order by a.time desc limit 0,20';
        $data = $this->query($sql);
        $status = array(1=>'待支付',2=>'正在送餐中',3=>'待收货',4=>'已完成',5=>'已取消');
        foreach ($data as $key => &$value) {
                $value['date'] = date('Y-m-d H:i',$value['time']);
                $value['status'] = $status[$value['order_status']];
                $cart = json_decode($value['cart'],true);
                foreach ($cart as $k => $v) {
                        $value['product_num'] += $v;
                        $productID = $k;
                        break;
                }
                $product = $this->query('select * from '.$left.'product where id='.$productID);
                $value['product_name'] = $product[0]['product_name'];
        }
  
                return $data;

        }
        /**
        * 获取用户所有订单
        * @param int $user_id 用户ID
        * @param string $type 订单 状态，1为待支付，2为待发货，3为待收货,4为已完成，5为已取消
        */
        public function getDetail($id){
                $left = C('DB_PREFIX');
                $field = 'a.time,a.order_status,a.pay_status,a.id as order_id,b.*,c.shop_name,c.logo';
                $sql = 'SELECT '.$field .' FROM '.$left.'order as a LEFT JOIN '.$left.'order_detail as b on a.order_number=b.order_number LEFT JOIN '.$left.'shop as c ON b.shop_id=c.id  WHERE b.id="'.$id.'" order by a.time desc';
                $data = $this->query($sql);
                $status = array(1=>'待支付',2=>'正在送餐中',3=>'待收货',4=>'已完成',5=>'已取消');
                foreach ($data as $key => &$value) {
                        $value['date'] = date('Y-m-d H:i',$value['time']);
                        $value['status'] = $status[$value['order_status']];
                }
                $data = $data[0];
                return $data;

        }
}
